<?

//Initiate Authenticate
SecurityRedirect ();

//initiate Vars
define("PRIMARY_KEY","kurssi_id");
$FileName        = "ais_transaction_currtaxrate.php";
$FileTemplate    = "ais_transaction_currtaxrate_form.html";
$HTMLFormName    = "CurrencyForm";
$TableName       = "ais_kurspjk";
$SubHeaderTitle  = "Transaction Currency Rate - Tax Rate";
$FormAction      = $FileName . "?" . AddParam(GetQueryString("QueryString", ""),"form","$HTMLFormName");
$FormEncType     = "application/x-www-form-urlencoded";
//$PMode         = "";

//Initiate Database Table For Insert & Update;
$FormFields     = array("tglbkt"=>"date",
                        "curr"=>"string",
                        "rate"=>"number"
                        );
buildsqlfield ($TableName, $FormFields);

//Leave Here Untouch
$FormCName     = BuildHTMLField($FormFields,"name",1);
$FormCValue    = BuildHTMLField($FormFields,"value",1);

//************ FORM ACTION **************
if (GetParam("form","")==$HTMLFormName) {
    if (GetParam("Action","")=="Add") {
        $FormMode="Edit";
        FormInsert();
    }
    if (GetParam("Action","")=="Update") {
        $FormMode="Edit";
        FormUpdate();
    }
    if (GetParam("Action","")=="Delete") {
        $FormMode="Edit";
        FormDelete();
    }
    if (GetParam("Action","")=="Cancel") {
        $FormMode="Edit";
        FormCancel();
    }
}

//Field Verification
$Verification  = "Enable";
$SessionVar    = md5(microtime());
//$SessionVar    = "Stuck";
$VerifyFields  = array("tglbkt"=>"Date","curr"=>"Currency","rate"=>"Rate");
VerifyHTML ($HTMLFormName,$VerifyFields,$SessionVar);

$InitFunction = "Masking()";
Init($SessionVar,BuildMasking ($HTMLFormName,$FormFields));
//************ SET FORM MODE ***************

if (GetParam(PRIMARY_KEY,"")) $FormMode = "Edit";
else $FormMode = "Add";

//******* DATABASE FORM QUERY ***********
//Rama, (01 September 2009), Start
//modified SQL, add one condition after "WHERE" clause
$SQLCurrency = "SELECT * FROM tblcurrency WHERE tusymbol NOT LIKE 'IDR'";
//Rama, (01 September 2009), End

$SQL = "SELECT * FROM `$TableName` ";
if (GetParam(PRIMARY_KEY,"")) {
    $SQLForm = $SQL."WHERE ".PRIMARY_KEY."='".GetParam(PRIMARY_KEY,"")."'";
}
//******* SHOW FORM CONTENT *********
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);

$TBS->MergeBlock("currblk",$DBMysql,$SQLCurrency);

if (GetParam(PRIMARY_KEY,"")) $TBS->MergeBlock("blv","mysql",$SQLForm);
else $TBS->MergeBlock("blv",$FormCValue);
//HTML Name
$TBS->MergeBlock("blc",$DBMysql,$SQL);
$TBS->MergeBlock("bln",$FormCName);
//HTMLGrid
$TBS->MergeBlock("blg",$DBMysql,$SQL);
$TBS->Show();


//*************** ACTION FUNCTION *******************
function FormInsert() {
    global $DBConnection,$FileName;
    $DBConnection->Insert();
    header("Location: $FileName?PageMode=Form");
    exit;
}

function FormUpdate() {
    global $DBConnection;
    $DBConnection->Update();
}

function FormDelete() {
    global $DBConnection,$FileName,$TableName;
    $DBConnection->Delete("$TableName",PRIMARY_KEY);
    header("Location: $FileName");
    exit;

}

function FormSearch () {

}

function FormCancel () {
    global $FileName;
    header("Location: ".$FileName);
    exit;
}
//*************** USER FUNCTION *******************
function buildsqlfield ($table, $arrayfield) {
        global $DBConnection;
        foreach ($arrayfield as $key=>$Formatting) {
            $value = GetParam($key,"");
            if ($Formatting=="date") {
                list($day,$month,$year) = explode("/",$value);
                $value = "$year-$month-$day";
            } elseif ($Formatting=="number") $value = str_replace(",","",$value);
            $DBConnection->add_htmlfield($table,$key,$value);
        }
}

?>
